package com.ayu.shortlink.engine.mq.producer;

import cn.hutool.core.util.StrUtil;
import com.ayu.shortlink.engine.common.constant.RocketMQConstant;
import com.ayu.shortlink.engine.mq.basics.base.BaseSendMessageExtendDTO;
import com.ayu.shortlink.engine.mq.basics.base.MessageWrapper;
import com.ayu.shortlink.engine.mq.basics.event.UserCouponOnTimeCloseEvent;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * 用户优惠券定时删除生产者 (将优惠券设置为不可用)
 */
@Component
public class UserCouponOnTimeCloseProducer extends AbstractCommonSendProduceTemplate<UserCouponOnTimeCloseEvent>{
    public UserCouponOnTimeCloseProducer(@Autowired RocketMQTemplate rocketMQTemplate) {
        super(rocketMQTemplate);
    }

    @Override
    protected BaseSendMessageExtendDTO buildBaseSendMessageExtendDTO(UserCouponOnTimeCloseEvent messageSendEvent) {
        return BaseSendMessageExtendDTO.builder()
                .eventName("用户优惠券定时删除")
                .topic(RocketMQConstant.TOPIC_USER_COUPON_ON_TIME_CLOSE)
                .sentTimeout(2000L)
                .build();
    }

    @Override
    protected Message<?> buildMessage(UserCouponOnTimeCloseEvent messageSendEvent, BaseSendMessageExtendDTO requestParam) {
        String keys = StrUtil.isBlank(requestParam.getKeys())? UUID.randomUUID().toString():requestParam.getKeys();
        return MessageBuilder
                .withPayload(new MessageWrapper(requestParam.getKeys(), messageSendEvent))
                .setHeader(MessageConst.PROPERTY_KEYS,keys)
                .setHeader(MessageConst.PROPERTY_TAGS, requestParam.getTag())
                .build();
    }
}
